package cn.wizzer.app.web.modules.controllers.platform.user;

import cn.wizzer.app.user.modules.models.enums.PurchaseEnum;
import cn.wizzer.app.user.modules.services.UserPurchaseService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.framework.util.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

@IocBean
@At("/platform/user/purchase")
public class PurchaseController {

    @Inject
    @Reference
    private UserPurchaseService purchaseService;

    @At("/")
    @Ok("beetl:/platform/user/purchase/index.html")
    @RequiresPermissions("user.purchase.list")
    public void index() {
    }


    /**
     * 购买记录查询
     *
     * @param status     String 兑换状态
     * @param pageNumber int
     * @param pageSize   int
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.purchase.list")
    public Object data(@Param("searchKeyword") String searchKeyword,
                       @Param("status") PurchaseEnum status,
                       @Param("searchDate") String[] searchDate,
                       @Param("pageNumber") int pageNumber,
                       @Param("pageSize") int pageSize) {
        return Result.success().addData(purchaseService.listPage(pageNumber, pageSize, searchKeyword, searchDate, status));
    }

    /**
     * 取消订单
     *
     * @param id String 订单id
     * @return Object
     */
    @At("/cancel/?")
    @Ok("json")
    @RequiresPermissions("user.purchase.edit")
    @SLog(tag = "取消订单", msg = "订单id:${args[0]}")
    public Object editDo(@Param("id") String id) {
        purchaseService.cancel(id);
        return Result.success();
    }


    @At("/finished/?")
    @Ok("json")
    @RequiresPermissions("user.purchase.edit")
    @SLog(tag = "订单完结", msg = "订单id:${args[0]}")
    public Object finished(@Param("id") String id) {
        purchaseService.update(Chain.make("status", PurchaseEnum.FINISHED), Cnd.where("id", "=", id));
        return Result.success();
    }

    @At("/doDelivery")
    @Ok("json")
    @RequiresPermissions("user.purchase.edit")
    @SLog(tag = "订单发货", msg = "订单id:${args[0]}")
    public Object doDelivery(@Param("id") String id,
                             @Param("logisticsCompany") String logisticsCompany,
                             @Param("logisticsNo") String logisticsNo,
                             @Param("remark") String remark) {
        purchaseService.update(Chain.make("status", PurchaseEnum.DELIVERY)
                        .add("logisticsCompany", logisticsCompany)
                        .add("logisticsNo", logisticsNo)
                        .add("remark", remark),
                Cnd.where("id", "=", id));
        return Result.success();
    }


    /**
     * 查询所有类型
     *
     * @return Object
     */
    @At("/statusList")
    @Ok("json")
    public Object statusList() {
        return Result.success().addData(PurchaseEnum.toList());
    }
}
